let globalSearchContent = '';
let globalCurrentPage = 1;
init();

function init() {
    getList(1);
}


function getList(page, searchContent='') {
    let url = '/?r=recruit/show-list';
    let data = {
        page: page,
        search: searchContent
    };
    globalCurrentPage = page;
    $.ajax({
        type: 'GET',
        url: url,
        data: data,
        success: function (data) {
            if (typeof (data) === 'string') {
                data = JSON.parse(data);
            }
            if (data['code'] === 200){
                data = data['data'];
                let listData = data['list'];
                _handlebars(listData, 'list-content-template', $('#list-content'));
                showPagination(data['count'], data['currentPage'])
            }else{
                alert('失败');
            }
        }
    });
}

function showOne(resumeID) {
    setMyModalTitle('编辑');
    setMyModalBody($('#edit-item'));
    $('#my-modal-submit').unbind('click');
    $('#my-modal-submit').click(function () {
        edit();
    });
    let prefix = 'item-' + resumeID + '-';
    let id = $('#' + prefix + 'id').val();
    let name = $('#' + prefix + 'name').html();
    let mobile = $('#' + prefix + 'mobile').html();
    let email = $('#' + prefix + 'email').html();
    let realFileName = $('#' + prefix + 'real-file-name').html();
    let data = {
        id: id,
        resume_id: resumeID,
        name: name,
        mobile: mobile,
        email: email,
        real_file_name: realFileName
    };
    _handlebars(data, 'edit-item-template', $('#my-modal-body'));
    // $('#edit-item-resume-id').val(resumeID);
    // $('#edit-item-name').val(name);
    // $('#edit-item-mobile').val(mobile);
    // $('#edit-item-email').val(email);
    // $('#edit-item-real-file-name').val(readFileName);
    $('#my-modal').modal('show')
}

function showAddOne() {
    setMyModalTitle('新增');
    setMyModalBody($('#edit-item'));
    $('#my-modal-submit').unbind('click');
    $('#my-modal-submit').click(function () {
        edit();
    });
    _handlebars({}, 'edit-item-template', $('#my-modal-body'));
    $('#my-modal').modal('show')
}


function showPagination(pageNum, currentPage) {
    pageNum = parseInt(pageNum);
    currentPage = parseInt(currentPage);
    if (pageNum <= 10) {
        _showPagination(1, pageNum, pageNum, currentPage);
        return;
    }
    if (currentPage <= 5) {
        _showPagination(1, 10, pageNum, currentPage);
        return;
    }
    if (currentPage >= pageNum - 5) {
        _showPagination(pageNum - 9, pageNum, pageNum, currentPage);
        return;
    }
    _showPagination(currentPage - 4, currentPage + 5, pageNum, currentPage);
}


function _showPagination(start, end, max, current) {
    let i = start;
    let rs = '';
    rs += `<li><a href="#" onclick="getList(1, globalSearchContent)"><span aria-hidden="true">&laquo;</span></a></li>`;
    for (i; i<= end; i++) {
        let liClass = i === current ? 'class="active"' : '';
        rs += `<li ${liClass}><a href="#" onclick="getList(${i}, globalSearchContent)">${i}</a></li>`;
    }
    rs += `<li><a href="#" onclick="getList(${max}, globalSearchContent)"><span aria-hidden="true">&raquo;</span></a></li>`;
    $('#page-pagination').html(rs);
}

function search() {
    let searchContent = $('#search').val();
    globalSearchContent = searchContent;
    getList(1, searchContent);
}

function edit() {
    let prefix = 'edit-item-';
    let id = $('#' + prefix + 'id').val();
    let resumeID = $('#' + prefix + 'resume-id').val();
    let name = $('#' + prefix + 'name').val();
    let mobile = $('#' + prefix + 'mobile').val();
    let email = $('#' + prefix + 'email').val();
    let realFileName = $('#' + prefix + 'real-file-name').val();

    let data = {
        id: id,
        resumeID: resumeID,
        name: name,
        mobile: mobile,
        email: email,
        realFileName: realFileName
    };

    url = '/?r=recruit/edit';
    $.ajax({
        type: 'GET',
        url: url,
        data: data,
        success: function (data) {
            if (typeof (data) === 'string') {
                data = JSON.parse(data);
            }
            if (data['code'] === 200){
                $('#my-modal').modal('hide');
                search();
            }else{
                alert('失败');
            }
        }
    });
}


function changeCheckStatus(id, preStatus) {
    let status = 0;
    if (preStatus === 0) {
        status = 1;
    }
    let url = '/?r=recruit/change-check-status';
    let data = {
        id: id,
        status: status
    };
    $.ajax({
        type: 'GET',
        url: url,
        data: data,
        success: function (data) {
            if (typeof (data) === 'string') {
                data = JSON.parse(data);
            }
            if (data['code'] === 200){
                getList(globalCurrentPage, globalSearchContent);
            }else{
                alert('失败');
            }
        }
    });
}

function exportExcel() {
    let url = '/?r=recruit/export';
    window.location.href = url;
}
